from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.contrib import admin


class Country(models.Model):
      id = models.CharField(_('id'), max_length=2, primary_key=True)
      name = models.CharField(_('name'),max_length=30)
      def __unicode__(self):
            return '%s(%s)' % (self.name, self.id)
      class Meta:
            ordering = ['name']
            verbose_name = _('country')
            verbose_name_plural = _('countries')


admin.site.register(Country)


class State(models.Model):
      id = models.CharField(_('id'), max_length=4, primary_key=True)
      name = models.CharField(_('name'),max_length=30)
      country = models.ForeignKey(Country, verbose_name = _('country'))
      def __unicode__(self):
            return self.name
      class Meta:
            ordering = ["name"]
            verbose_name = _('state')
            verbose_name_plural = _('states')


class StateAdmin(admin.ModelAdmin):
      list_display = ['id', 'name', 'country']


admin.site.register(State, StateAdmin)


class Title(models.Model):
      name = models.CharField(_('name'),max_length=10)
      def __unicode__(self):
            return self.name
      class Meta:
            ordering = ["name"]
            verbose_name = _('title')
            verbose_name_plural = _('titles')


admin.site.register(Title)


class Rol(models.Model):
      name = models.CharField(_('name'),max_length=20)
      def __unicode__(self):
            return self.name
      class Meta:
            ordering = ["name"]
            verbose_name = _('rol')
            verbose_name_plural = _('rols')


admin.site.register(Rol)


class Tax(models.Model):
      id = models.CharField(_('tax'), max_length=5, primary_key=True)
      name = models.CharField(_('name'),max_length=30)
      percent = models.DecimalField(_('percent'), max_digits=5, decimal_places=2)
      def __unicode__(self):
            return self.name
      class Meta:
            ordering = ["name"]
            verbose_name = _('tax')
            verbose_name_plural = _('taxes')


class TaxAdmin(admin.ModelAdmin):
      list_display = ['id' , 'name', 'percent']


admin.site.register(Tax, TaxAdmin)


class Currency(models.Model):
      id = models.CharField(_('id'), max_length=3, primary_key=True)
      name = models.CharField(_('name'),max_length=30)
      rate = models.DecimalField(_('rate'), max_digits=14, decimal_places=2)
      def __unicode__(self):
            return self.name
      class Meta:
            ordering = ["name"]
            verbose_name = _('currency')
            verbose_name_plural = _('currencies')


class CurrencyAdmin(admin.ModelAdmin):
            list_display = ['id' , 'name', 'rate']


admin.site.register(Currency, CurrencyAdmin)


class Phone(models.Model):
      TELEPHONE_CHOICES = (
              ('G', _('general')),
              ('H', _('home')),
              ('W', _('work')),
              ('C', _('cellular')),
              ('F', _('fax')),
              ('O', _('other')),
      )
      phone_type = models.CharField(_('phone type'), max_length=1, choices=TELEPHONE_CHOICES)
      number = models.CharField(_('number'), max_length=15, core = True)
      extension = models.CharField(_('extension'), max_length=5, core = False, blank = True, null = True)
      def __unicode__(self):
            return self.number
      class Meta:
            abstract = True
            verbose_name = _('phone')
            verbose_name_plural = _('phones')

class AddressType(models.Model):
      id = models.CharField(_('id'), max_length=2, primary_key=True)
      name = models.CharField(_('name'),max_length=30)
      def __unicode__(self):
            return '%s %s' % (self.id, self.name)
      class Meta:
            ordering = ['name']
            verbose_name = _('address type')
            verbose_name_plural = _('address types')


admin.site.register(AddressType)


class Address(models.Model):
      type = models.ForeignKey(AddressType, verbose_name = _('address type'))
      street = models.CharField(_('street'), max_length=50, core = True)
      extnum = models.CharField(_('exterior number'), max_length=10)
      intnum = models.CharField(_('interior number'), max_length=10, blank = True, null = True)
      neighborhood  = models.CharField(_('neighborhood'), max_length=50)
      district = models.CharField(_('district'), max_length=30, blank = True, null = True)
      city = models.CharField(_('city'), max_length=30)
      postal_code = models.CharField(_('postal code'), max_length=5)
      state = models.ForeignKey(State, verbose_name = _('state'))
      country = models.ForeignKey(Country, verbose_name = _('country'))
      class Meta:
            verbose_name = _('address')
            verbose_name_plural = _('addresses')
            abstract = True


class Company(models.Model):
      name = models.CharField(_('name'), max_length=100, core = True)
      taxpayer_code = models.CharField(_('taxpayer code'), max_length=15, blank = True, null = True)
      comercial_name = models.CharField(_('comercial name'), max_length=50, blank = True, null = True)
      website = models.URLField(_('website'), blank = True, null = True)
      def __unicode__(self):
            return self.name
      class Meta:
            ordering = ["name"]
            verbose_name = _('company')
            verbose_name_plural = _('companies')


class CompanyPhone(Phone):
      company_id = models.ForeignKey(Company)
      class Meta:
            verbose_name = _('company phone')
            verbose_name_plural = _('company phones')


class CompanyAddress(Address):
      company_id = models.ForeignKey(Company)
      class Meta:
            verbose_name = _('company address')
            verbose_name_plural = _('company addresses')


class CompanyPhoneInline(admin.TabularInline):
      model = CompanyPhone
      extra = 1

class CompanyAddressInline(admin.TabularInline):
      model = CompanyAddress
      extra = 1


class CompanyAdmin(admin.ModelAdmin):
      inlines = [CompanyPhoneInline, CompanyAddressInline]


admin.site.register(Company, CompanyAdmin)


class Person(models.Model):
      name = models.CharField(_('name'), max_length=30, core=True)
      paternal = models.CharField(_('paternal lastname'), max_length=30)
      maternal = models.CharField(_('maternal lastname'),max_length=30, blank = True, null = True)
      title = models.ForeignKey(Title, blank = True, null = True, verbose_name = _('title'))
      company = models.ForeignKey(Company, blank = True, null = True, verbose_name = _('company'))
      job = models.CharField(_('job'), max_length=20, blank = True, null = True)
      rol = models.ForeignKey(Rol, verbose_name = _('rol'))
      email = models.EmailField(_('email'), blank = True, null = True)
      def __unicode__(self):
            return '%s %s %s %s' % (self.title, self.name, self.paternal, self.maternal)
      class Meta:
            ordering = ["paternal", "maternal", "name"]
            verbose_name = _('person')
            verbose_name_plural = _('persons')


admin.site.register(Person)


class PersonPhone(Phone):
      person_id = models.ForeignKey(Person, edit_inline=models.TABULAR)
      class Meta:
            verbose_name = _('person phone')
            verbose_name_plural = _('person phones')


class PersonAddress(Address):
      person_id = models.ForeignKey(Person, edit_inline=models.TABULAR)
      class Meta:
            verbose_name = _('person address')
            verbose_name_plural = _('person addresses')

